AddLeaveRequestController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 39
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A get 0 7 1
A post 0 25 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards,
8
  Render,
9
  Get,
10
  Res
11
} from '@nestjs/common';
12
import { Response } from 'express';
13
import { ICommandBus } from 'src/Application/ICommandBus';
14
import { LeaveRequestDTO } from '../DTO/LeaveRequestDTO';
15
import { CreateLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/CreateLeaveRequestCommand';
16
import { LoggedUser } from '../../User/Decorator/LoggedUser';
17
import { IsAuthenticatedGuard } from '../../User/Security/IsAuthenticatedGuard';
18
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
19
import { User } from 'src/Domain/HumanResource/User/User.entity';
20
import { getSelectableLeaveRequestTypes } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
21
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
22
23
@Controller('app/people/leave-requests/add')
24
@UseGuards(IsAuthenticatedGuard)
25
export class AddLeaveRequestController {
26
  constructor(
27
    @Inject('ICommandBus')
28
    private readonly commandBus: ICommandBus,
29
    private readonly resolver: RouteNameResolver
30
  ) {}
31
32
  @Get()
33
  @WithName('people_leave_requests_add')
34
  @Render('pages/leave_requests/add.njk')
35
  public async get() {
36
    return {
37
      types: getSelectableLeaveRequestTypes()
38
    };
39
  }
40
41
  @Post()
42
  public async post(
43
    @Body() dto: LeaveRequestDTO,
44
    @LoggedUser() user: User,
45
    @Res() res: Response
46
  ) {
47
    const { type, startDate, startsAllDay, endDate, endsAllDay, comment } = dto;
48
49
    try {
50
      await this.commandBus.execute(
51
        new CreateLeaveRequestCommand(
52
          user,
53
          type,
54
          startDate,
55
          startsAllDay,
56
          endDate,
57
          endsAllDay,
58
          comment
59
        )
60
      );
61
62
      res.redirect(303, this.resolver.resolve('people_leave_requests_list'));
63
    } catch (e) {
64
      throw new BadRequestException(e.message);
65
    }
66
  }
67
}
68